package org.jeecg.modules.ssqy.controller;


import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.sczt.entity.SpatialQueryParam;
import org.jeecg.modules.ssqy.entity.Ssqy;
import org.jeecg.modules.ssqy.service.ISsqyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 四上企业模块接口
 *
 * @author: scott
 */
@Slf4j
@RestController
@RequestMapping("/cydt/ssqy")
@Api(tags = "四上企业分析")
public class SsqyController {

    /**
     * 注入书签服务
     */
    @Autowired
    private ISsqyService ssqyService;


    /**
     * 根据Sczt获取
     *
     * @param shxydm:shxydm
     * @return Result<Ssqy>
     */
    @ApiOperation(value = "根据社会信用代码获取四上企业信息")
    @ApiOperationSupport(order=1)
    @RequestMapping(value = "/getScztByShxydm", method = RequestMethod.GET)
    public Result<Ssqy> getScztByShxydm(@RequestParam(name = "shxydm") String shxydm) {

        Result<Ssqy> result = new Result<>();
        Ssqy sczt = ssqyService.geScztByShxydm(shxydm);

        if (sczt == null) {
            result.error500("当前企业不存在");
        } else {
            result.setResult(sczt);
            result.setSuccess(true);
        }
        return result;

    }

    /**
     * 获取所有四上企业
     *
     * @return
     */
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
//    @ApiOperation(value = "获取所有四上企业")
    public Result<List<Ssqy>> listAll() {
        Result<List<Ssqy>> result = new Result<>();
        List<Ssqy> list = ssqyService.listAll();

        if (list == null) {
            result.error500("获取所有四上企业");
        } else {
            result.setResult(list);
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 分页获取四上企业信息列表(可按名称查询)
     * @param pageNum：页码
     * @param pageSize：每页记录条数
     * @return
     */
    @RequestMapping(value = "/list" ,method = RequestMethod.GET)
    @ApiOperation(value = "分页获取四上企业信息列表")
    @ApiOperationSupport(order=2)
    public Result  list(@RequestParam(name = "pageNum") Number pageNum,
                                   @RequestParam(name = "pageSize") Number pageSize,
                                    @RequestParam(name = "nsrmc",required = false) String nsrmc) {
        Result result = new Result<>();
        JSONObject jsonObject = ssqyService.list(pageNum,pageSize,nsrmc);

        if (jsonObject == null) {
            result.error500("获取所有四上企业");
        } else {
            result.setResult(jsonObject);
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 分页获取四上企业信息列表
     * @param pageNum：页码
     * @param pageSize：每页记录条数
     * @return
     */
    @RequestMapping(value = "/getScztGeoJson" ,method = RequestMethod.GET)
    @ApiOperation(value = "分页获取四上企业GeoJSON数据")
    @ApiOperationSupport(order=3)
    public Result  getScztGeoJson(@RequestParam(name = "pageNum") Number pageNum,
                        @RequestParam(name = "pageSize") Number pageSize,
                                  @RequestParam(name = "nsrmc",required = false) String nsrmc) {
        Result result = new Result<>();
        JSONObject jsonObject = ssqyService.list(pageNum,pageSize,nsrmc);

        if (jsonObject == null) {
            result.error500("获取所有四上企业");
        } else {
            result.setResult(jsonObject);
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 更新四上企业信息【根据ID】
     * @param sczt
     * @return
     */
    @RequestMapping(value = "/updateSczt", method = RequestMethod.POST)
    @ApiOperation(value = "更新四上企业信息")
    @ApiOperationSupport(order=4)
    @ResponseBody
    public Result updateSczt(@RequestBody Ssqy sczt){
        Result result = new Result<>();
        Boolean flag= ssqyService.updateSczt(sczt);
        if (flag){
            result.setSuccess(true);
            result.setResult(sczt);
        }else{
            result.error500("更新失败！");
        }
        return  result;
    }

    /**
     * 根据空间范围获取四上企业信息
     * @param polygon
     * @return
     */
    @ApiOperation(value = "根据空间范围获取四上企业信息列表")
    @ApiOperationSupport(order=5)
    @RequestMapping(value = "/getScztByPolygon",method = RequestMethod.POST)
    @ResponseBody
    public Result getScztByPolygon(@RequestBody SpatialQueryParam params){
        Result result = new Result<>();
        System.out.println("传入的polygon>>>>>>>>:"+params.getPolygon());

        List<Map> scztFeatureList = ssqyService.getScztByPolygon(JSONObject.toJSONString(params.getPolygon()));
        List features2=new ArrayList();

        for (int i = 0; i < scztFeatureList.size(); i++) {
            features2.add(JSONObject.parse(scztFeatureList.get(i).get("feature").toString()));
        }
        JSONObject featureCollection= new JSONObject();
        featureCollection.put("type","FeatureCollection");
        featureCollection.put("features",features2);
        result.setResult(featureCollection);
        return result;
    }

}
